package B3;

import com.github.oxo42.stateless4j.StateMachine;
import com.github.oxo42.stateless4j.StateMachineConfig;

public class Read_source_file {

    private StateMachineConfig<State, Event> config;
    private GuardCondition guardCondition = new GuardCondition();
    private StateMachine<State, Event> stateMachine;
    public Read_source_file() {
        init();
    }

    public void init() {
        guardCondition.setIsstr(false);
        guardCondition.setIsescape(false);

        // 创建状态机配置
        config = new StateMachineConfig<>();

        // 定义状态转换规则
        config.configure(State.Reading)
                .permitIf(Event.toReading_str,State.Reading_str,guardCondition.isString(),new StartReading_strAction())
                .ignoreIf(Event.toReading_str,guardCondition.isnotString())
                .ignore(Event.toReading);

        config.configure(State.Reading_str)
                .permitIf(Event.toReading,State.Reading,guardCondition.isString(),new StopReading_strAction())
                .permitIf(Event.toescape_next_characher,State.escape_next_character,guardCondition.isEscape(),new EscapeAction())
                .ignore(Event.toReading_str);

        config.configure(State.escape_next_character)
                .permit(Event.toReading_str,State.Reading_str)
                .ignore(Event.toReading)
                .ignore(Event.toescape_next_characher);

        // 创建状态机实例
        stateMachine = new StateMachine<>(State.Reading, config);
    }

    public void setstr(boolean isstr){
        guardCondition.setIsstr(isstr);
    }
    public void setescape(boolean isescape){
        guardCondition.setIsescape(isescape);
    }
    public void fire(Event event) {
        this.stateMachine.fire(event);
    }
    public State getCurrentState() {
        return this.stateMachine.getState();
    }
    public void printCurrentState() {
        System.out.println("Current State: " + stateMachine.getState());
    }
}
